﻿using Janus.BaseEntitys;
using Janus.Cores.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace Janus.Cores
{
    /// <summary>
    /// 存储过程分页
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    public class PageCriteriaer<TEntity> where TEntity : Entity
    {
        /// <summary>
        /// 存储过程分页
        /// </summary>
        /// <param name="predicate">条件</param>
        /// <param name="pagination">分页对象</param>
        /// <param name="primaryKey">排序主键</param>
        public PageCriteriaer(Expression<Func<TEntity, bool>> predicate, PaginationDTO pagination, string primaryKey)
        {
            this.PageCriteria = new PageCriteria() { };
            this.PageCriteria.PageSize = pagination.PageSize;
            this.PageCriteria.CurrentPage = pagination.CurrentIndex;
            this.PageCriteria.PrimaryKey = primaryKey;
            this.PageCriteria.RecordCount = pagination.RowCount.HasValue ? pagination.RowCount.Value : 0;
            this.PageCriteria.TableName = typeof(TEntity).Name;
            this.PageCriteria.Condition = predicate.ToWhere();
           //this.PageCriteria.Sort = Orders;
        }

        /// <summary>
        /// 分页存储过程使用
        /// </summary>
        public PageCriteria PageCriteria
        { get; set; }

        //public string Orders
        //{
        //    get; private set;
        //}

        public PageCriteriaer<TEntity> OrderBy(Expression<Func<TEntity, object>> order)
        {
            var name = order.GetName();
            this.PageCriteria.Sort += string.Format("{0} ASC", string.IsNullOrEmpty(this.PageCriteria.Sort) ? " [" + name + "]" : ",[" + name + "]");
            return this;
        }

        public PageCriteriaer<TEntity> OrderDescBy(Expression<Func<TEntity, object>> order)
        {
            var name = order.GetName();
            this.PageCriteria.Sort += string.Format("{0} DESC", string.IsNullOrEmpty(this.PageCriteria.Sort) ? " [" + name + "]" : ",[" + name + "]");
            return this;
        }

    }
}
